package cn.dengta.webapp.trade.vo;

import java.util.List;
import java.util.Map;

import cn.dengta.common.model.APIResponse.Field;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.trade.entity.UserBagAvatar;
import cn.dengta.webapp.trade.entity.UserLevel;
import cn.dengta.webapp.trade.model.UserLevelType;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.tuple.Pair;

@Getter
@Setter
public class UserLevelVO extends AbstractVO<UserLevel> {

	/** 总经验 */
	private Long exp;
	/** 下一等级经验 */
	private Long nextExp;
	/** 等级 */
	private Integer level;
	/** 下一等级 */
	private Integer nextLevel;
	private Map<String, Object> user;
	private List<UserBagAvatar> avatar;
	private String currentAvatar;

	public static UserLevelVO from(UserLevel source) {
		return copy(source, UserLevelVO::new);
	}

	public static UserLevelVO fromLevel(UserLevel source) {
		UserLevelVO vo = copy(source, UserLevelVO::new);
		if (vo != null) {
			Pair<Integer, Long> pair = UserLevelType.calcLevel(source.getExp());
			Pair<Integer, Long> nextPair = UserLevelType.calcNextLevel(source.getExp(), pair);
			vo.setLevel(pair.getLeft());
			vo.setNextLevel(nextPair.getLeft());
			vo.setNextExp(nextPair.getRight());
		}
		return vo;
	}

	public static UserLevelVO init() {
		UserLevelVO vo = new UserLevelVO();
		vo.exp = 0L;
		vo.level = UserLevelType.L0.getValue();
		vo.nextLevel = UserLevelType.L1.getValue();
		vo.nextExp = UserLevelType.L1.getExp();
		return vo;
	}

	@Override
	public String getAddTime_() {
		return super.getAddTime_();
	}

	@Setter
	public static class AvatarEquity {

		@Field("等级")
		public Integer level;
		@Field("头像")
		public String avatar;
		@Field("ID")
		public Long bagId;
		@Field("是否拥有")
		public Boolean owned;
		@Field("是否选中")
		public Boolean select;

		public AvatarEquity init(Integer level, String avatar, Long bagId, Boolean owned, Boolean select) {
			this.level = level;
			this.avatar = avatar;
			this.bagId = bagId;
			this.owned = owned;
			this.select = select;
			return this;
		}

	}

}
